<?php

declare(strict_types=1);

namespace app\controller;

use think\facade\Db;
use think\Request;

use app\model\User as UserModel;
use app\model\UserAccount as UserAccount;
use app\model\TempCode as TempCode;

class User extends Base
{


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function usersList()
    {
        $post = request()->param();
        $user_model = new UserModel();
        $res = $user_model->getAll($post['list_rows'], false, ['query' => $post], $post);
        if ($res[0] !== true) {
            return $this->create($res[1] , '失败', 204);
        }
        // $total = $user_model->getTotal()[1];
        // return $info;
        // $data = [
        //     'work_num'   => $info['work_num'],
        //     'name'       => $info['name'],
        //     'age'        => $info['age'],
        //     'sex'        => $info['sex'],
        //     'position'   => $info['position'],
        //     'department' => $info['department']
        // ];
        return $this->create($res[1] , '成功');
    }

    public function updateRoles()
    {
        $post = request()->param();
        $user_model = new UserModel();
        $user_model->updateRoles($post['work_num'], $post['roles']);
        return $this->create('', '成功');
    }

    public function updateStatus()
    {
        $post = request()->param();

        $user_model = new UserModel();
        $data = $user_model->updateStatus($post['work_num'], $post['status']);
        // return $data[1];
        if ($data[0] === 'false') {
            return $this->create($data[1], '失败', 20001);
        }
        return $this->create('', '成功');
    }

    // 个人主页
    public function getUser()
    {
        $post = request()->param();
        $user_model = new UserModel();
        $account_model = new UserAccount();
        $info = $user_model->getInfoByWorkNum($post['work_num']);
        $avatar = $account_model->getInfoByWorkNum($post['work_num'])[1]['avatar'];
        if ($info[0] !== true) {
            return $this->create($info[1] , '失败', 204);
        }
        $data = [
            'name'   => $info[1]['name'],
            'roles'  => [$info[1]['roles']],
            'avatar' => $avatar,
            'sex'    => $info[1]['sex'],
            'age'    => $info[1]['age'],
            'position'    => $info[1]['position'],
            'department'    => $info[1]['department'],
            'create_time'    => $info[1]['create_time'],
            'email'    => $info[1]['email'],
            'work_num'    => $info[1]['work_num'],
        ];
        return $this->create( $data , '成功');
    }





    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        // table方法必须指定完整的数据表名
        $token = Db::table('vue_test')->where('id', 1)->find();
        return json($token);
    }
    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->param();
        $token = Db::table('vue_test')->where('name', $data['name'])->find();
        return json(['code' => 1, 'msg' => 'ok', 'data' => $token], 200);
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    public function read($id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
